/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.CFields;
import DTO.CKhachHang;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CKhachHangDAO extends IData<CKhachHang>{
   public  CKhachHangDAO(){}

    @Override
    protected Object OnSubmit(CKhachHang item, int t, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected CKhachHang getItem(ResultSet rs) {
        CKhachHang item = new CKhachHang();
       try {
           item.setDiaChi(rs.getString(CFields.dia_chi));
           item.setHoTen(rs.getString(CFields.ten));
           item.setId(rs.getInt(CFields.id));
           item.setDienThoai(rs.getString(CFields.dien_thoai));
           item.setFax(rs.getString(CFields.fax));
           item.setLoai(rs.getInt(CFields.loai));
           item.setMaThe(rs.getString(CFields.ma_the));
           item.setNhomKhId(rs.getLong(CFields.nhom_khach_hang_id));
       } catch (SQLException ex) {
           Logger.getLogger(CKhachHangDAO.class.getName()).log(Level.SEVERE, null, ex);
       }
        return item;
    }

    @Override
    public List<CKhachHang> getAll(SubConfig config) {
        String mysql = "select * from  vKhachHang " + " " + this.sWhere;
        return this.getAll(mysql, config);
    }

    @Override
    public Object insertOnSubmit(CKhachHang item, SubConfig config) {
        Object result = 0;
        String mysql = "insert into " + CFields.tb_khach_hang +"("
                + CFields.loai + "," + CFields.ten + ","
                + CFields.dia_chi + "," + CFields.dien_thoai + ","
                + CFields.diem_tich_luy + "," + CFields.nhom_khach_hang_id
                +") values(?,?,?,?,?,?)";
        System.out.println("" + mysql);
       try {
           PreparedStatement cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
           cs.setInt(1, item.getLoai());
           cs.setString(2, item.getHoTen());
           cs.setString(3, item.getDiaChi());
           cs.setString(4, item.getDienThoai());
           cs.setInt(5, 0);
           cs.setLong(6, item.getNhomKhId());
           cs.executeUpdate();
           ResultSet keyResultSet = cs.getGeneratedKeys();
            if (keyResultSet.next()) {
                result = (long) keyResultSet.getInt(CFields.id);     
            }
       } catch (SQLException ex) {
           Logger.getLogger(CKhachHangDAO.class.getName()).log(Level.SEVERE, null, ex);
           throw new UnsupportedOperationException(ex.getMessage()); //To change body of generated methods, choose Tools | Templates.
      }
     
        return result;
    }

    public void updateDiem(int diem,long khach_id, SubConfig config){
        String mysql ="update " + CFields.tb_khach_hang + " set ";
        mysql = mysql + " diem_tich_luy = diem_tich_luy + ?  where id =?";
       try {
           PreparedStatement cs = config.connec.prepareStatement(mysql);
           cs.setInt(1, diem);
           cs.setLong(2, khach_id);
           cs.executeUpdate();
       } catch (SQLException ex) {
           Logger.getLogger(CKhachHangDAO.class.getName()).log(Level.SEVERE, null, ex);
       }
        
    }
    @Override
    public Object updateOnSubmint(CKhachHang item, SubConfig config) {
        Object result = 0;
        String mysql ="update " + CFields.tb_khach_hang + " set "
                + CFields.loai + "=?,"+ CFields.ten + "=?,"
                + CFields.dia_chi + "=?," + CFields.dien_thoai + "=?,"
                + CFields.fax + "=?," + CFields.diem_tich_luy + "=?,"
                + CFields.ma_the + "=?," + CFields.nhom_khach_hang_id +"=? where "
                +CFields.id + "=?";                
       try {
           PreparedStatement cs = config.connec.prepareStatement(mysql);
           cs.setInt(1, item.getLoai());
           cs.setString(2, item.getHoTen());
           cs.setString(3, item.getDiaChi());
           cs.setString(4, item.getDienThoai());
           cs.setString(5, item.getFax());
           cs.setInt(6, item.getDiemTichLuy());
           cs.setString(7, item.getMaThe());
           cs.setLong(8, item.getNhomKhId());
           cs.setLong(9, item.getId());
           result = cs.executeUpdate();
       } catch (SQLException ex) {
           Logger.getLogger(CKhachHangDAO.class.getName()).log(Level.SEVERE, null, ex);
           throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
       }
        
        return result;
    }

    @Override
    public Object deleteOnSubmint(CKhachHang item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

   
    @Override
    public CKhachHang Find(CKhachHang item, List<CKhachHang> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public CKhachHang add(CKhachHang item) {
       try {
           ps.setInt(1, item.getLoai());
           ps.setString(2, item.getHoTen());
           ps.setString(3, item.getDiaChi());
           ps.setString(4, item.getDienThoai());
           ps.setInt(5, 0);
           ps.setLong(6, item.getNhomKhId());
           ps.setString(7, item.getMaThe());
           ps.setString(8, item.getFax());
           ps.addBatch();
       } catch (SQLException ex) {
           Logger.getLogger(CKhachHangDAO.class.getName()).log(Level.SEVERE, null, ex);
       }
       return item;
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int addrows(List<CKhachHang> list, SubConfig config) {
          String mysql = "insert into " + CFields.tb_khach_hang +"("
                + CFields.loai + "," + CFields.ten + ","
                + CFields.dia_chi + "," + CFields.dien_thoai + ","
                + CFields.diem_tich_luy + "," + CFields.nhom_khach_hang_id + ","
                  + CFields.ma_the + "," + CFields.fax
                +") values(?,?,?,?,?,?,?,?)";
        int rs = 0;
       try {
           ps = config.connec.prepareStatement(mysql);
           for(CKhachHang item : list){
                this.add(item);
            }    
           int []n = ps.executeBatch();
           rs = n.length;
           ps.close();
       } catch (SQLException ex) {
           Logger.getLogger(CKhachHangDAO.class.getName()).log(Level.SEVERE, null, ex);
       }
       return rs;
    }

    @Override
    protected void add(CKhachHang item, int t) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

   
}
